﻿using System;
using System.Collections.Generic;
using System.Text;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.Persist.DAL.Dao;
using Telegnosis.Framework.DataAccess.DB;

namespace CharacterTrailLibrary.Impl
{
    public class BarCodeTracking : IBarCodeTracking
    {
        /// <summary>
        /// 逻辑：先找header，如果找到，再从detail表中找物料看是否存在，如果存在，找到父亲，继续
        /// 如果在header中没有，则找detail表中：
        /// 从detail表中找物料看是否存在该barcode，如果存在，找到父亲，循环
        /// 目标：找到与该barcode相关的产品的最终成品的barcode
        /// </summary>
        /// <param name="barCode"></param>
        /// <returns></returns>
        public string GetTopProductByBarCode(string barCode)
        {
            string tmpBarCode = barCode;
            string resultBarCode = string.Empty;
            
            while (true)
            {
                string tmp = GetParentProductBarCode(tmpBarCode);
                if(String.IsNullOrEmpty(tmp) )
                {
                    break;
                }
                else
                {
                    tmpBarCode = tmp;
                    resultBarCode = tmp;
                    continue;
                }
            }

            if(!String.IsNullOrEmpty(resultBarCode))
            {
                return resultBarCode;
            }

            //先找是否存在该产品
            IProductPersist productPersist = InstanceFactory.GetProductPersist();
            ////因为不同的工序可能有不同的产品实例，所以可能存在多条记录，这里只是第一条记录
            var productHeaderModel = productPersist.GetProductHeaderModel(barCode);
            if (productHeaderModel != null)
            {
                resultBarCode = productHeaderModel.ProductItemBarCode;
            }

            return resultBarCode;
        }

// ReSharper disable MemberCanBeMadeStatic.Local
        private String GetParentProductBarCode(string barCode)
// ReSharper restore MemberCanBeMadeStatic.Local
        {
            var sqlWhere = new SqlItem();
            const string condition = " {0}='{1}' ";
            string condition1 = string.Format(condition, "ChildItemBarCode", barCode);
            sqlWhere.Text = condition1;
            var daoDetail = new TProductDetailDao();
            var list = daoDetail.GetList(sqlWhere);
            if (list.Count == 0)
            {
                return string.Empty;
            }
            else
            {
                return list[0].ParentProductItemBarCode;
            }
        }

        /// <summary>
        /// 根据最终产品的barcode，搜索所有相关的串号，信息，列出来
        /// </summary>
        /// <param name="productBarCode"></param>
        /// <returns></returns>
        public List<VProductDetailHeaderBaseModel> GetVProductDetailHeaderBaseModels(string productBarCode)
        {
            var list = new List<VProductDetailHeaderBaseModel>();

            GetChildProductModels(productBarCode, list);

            return list;
        }

// ReSharper disable MemberCanBeMadeStatic.Local
        private void GetChildProductModels(string parentBarCode,List<VProductDetailHeaderBaseModel> list )
// ReSharper restore MemberCanBeMadeStatic.Local
        {
            var sqlWhere = new SqlItem();
            string condition = " ParentProductItemBarCode='{0}' ";
            condition = string.Format(condition, parentBarCode);
            sqlWhere.Text = condition;
            var dao = new VProductDetailHeaderBaseDao();
            var list2 = dao.GetList(sqlWhere);
            foreach (var vProductDetailHeaderBaseModel in list2)
            {
                list.Add(vProductDetailHeaderBaseModel);
                GetChildProductModels(vProductDetailHeaderBaseModel.ChildItemBarCode,list);
            }
        }

        /// <summary>
        /// 根据barcode搜索相关的维修动作
        /// </summary>
        /// <param name="barCode"></param>
        /// <returns></returns>
        public VRepairBaseModel GetVRepairBaseModel(string barCode)
        {
            var sqlWhere = new SqlItem();
            string condition = " ProductItemBarCode='{0}' ";
            condition = string.Format(condition, barCode);
            sqlWhere.Text = condition;
            var dao = new VRepairBaseDao();
            var list = dao.GetList(sqlWhere);
            if (list.Count == 0)
                return null;
            return list[0];
        }


        public List<VRepairBaseModel> GetRelateVRepairBaseModels(List<VProductDetailHeaderBaseModel> list)
        {
            var resultList = new List<VRepairBaseModel>();
            foreach (var vProductDetailHeaderBaseModel in list)
            {
                var obj = GetVRepairBaseModel(vProductDetailHeaderBaseModel.ChildItemBarCode);
                if(obj != null)
                    resultList.Add(obj);
            }
            return resultList;
        }


        public List<VProductDetailHeaderBaseModel> GetVProductDetailHeaderBaseModelsByCondition(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var dao = new VProductDetailHeaderBaseDao();
            return dao.GetList(sqlWhere);
        }
    }
}
